import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_my_web/pages/tools/drop_draw_board/base/DropBean.dart';
import 'package:flutter_my_web/pages/tools/drop_draw_board/event/refresh_draw_board_event.dart';
import 'package:get/get.dart';

import '../event/show_property_panel_event.dart';

class AutoRefresh extends GetxController {
  StreamSubscription? refreshDrawBoardEventListen;

  @override
  void onInit() {
    super.onInit();
    refreshDrawBoardEventListen = refreshDrawBoardEvent.on().listen((event) {
      refresh();
    });
  }
// @override
// void onClose() {
//   super.onClose();
//   refreshDrawBoardEventListen?.cancel();
// }
}

abstract class BaseDropWidget<T extends DropBean> extends StatelessWidget {
  T dropBean;

  BaseDropWidget(this.dropBean);

  // BaseDropWidget(this.dropBean):super(key: ValueKey(Random().nextInt(100000)));

  var mouseing = false.obs;

  var autoRefresh = AutoRefresh();

  Widget buildChildWidget();

  @override
  Widget build(BuildContext context) {
    return LongPressDraggable(
      data: this,
      onDragStarted: () { // 开始拖动回调
        // print('=== onDragStarted');
        mouseing.value = false;
      },
      // 接收失败回调
      onDraggableCanceled: (Velocity velocity, Offset offset) {
        // print('=== onDraggableCanceled');
        mouseing.value = false;
      },
      onDragCompleted: () { // 接收成功回调
        // print('=== onDragCompleted');
        mouseing.value = false;
      },
      feedback: Material( //拖动中的颜色
        color: Colors.transparent,
        child: GetBuilder<AutoRefresh>(
          init: autoRefresh,
          assignId: true,
          builder: (logic) {
            return Container(
              height: dropBean.height,
              width: dropBean.width,
              decoration: BoxDecoration(
                // borderRadius: dropBean.borderRadius,
                // color: dropBean.backColor,
                  color: Colors.white
              ),
              // alignment: dropBean.alignment,
              child: buildChildWidget(),
            );
          },
        ),
      ),
      childWhenDragging: //产生拖动时，原位置控件颜色
      GetBuilder<AutoRefresh>(
        init: autoRefresh,
        assignId: true,
        builder: (logic) {
          return Container(
            height: dropBean.height,
            width: dropBean.width,
            decoration: BoxDecoration(
              // borderRadius: dropBean.borderRadius,
              // color: dropBean.backColor,
                color: Colors.white
            ),
            // alignment: dropBean.alignment,
            child: buildChildWidget(),
          );
        },
      ),
      child: GestureDetector(
        onTap: () {
          if (dropBean.drawBoardId > 0) {
            showPropertyPanel(dropBean);
          }
        },
        child: MouseRegion(
          onEnter: (event) {
            mouseing.value = true;
          },
          onExit: (event) {
            mouseing.value = false;
          },
          child: GetBuilder<AutoRefresh>(
            init: autoRefresh,
            assignId: true,
            builder: (logic) {
              return Obx(() {
                return Container(
                  height: dropBean.height,
                  width: dropBean.width,
                  decoration: BoxDecoration(
                    // borderRadius: dropBean.borderRadius,
                    // color: mouseing.value?Colors.red:dropBean.backColor,
                    color: mouseing.value ? Colors.red : Colors.white,
                  ),
                  // alignment: dropBean.alignment,
                  child: buildChildWidget(),
                );
              });
            },
          ),
        ),
      ),
    );
  }
}

